package skean.apserver;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.lang.reflect.Method;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiManager;
import android.net.wifi.WifiManager.WifiLock;
import android.util.Log;

public class WifiAdmin {
	// 定义WifiManager对象
	private WifiManager wifiManager;
	private WifiLock mWifiLock;
	private List<String> linkedList;

	// 构造器
	public WifiAdmin(Context context) {
		// 取得WifiManager对象
		wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
		creatWifiLock();
	}

	// wifi热点开关
	public boolean setWifiApEnabled(boolean enabled) {
		if (enabled) { // disable WiFi in any case
			// wifi和热点不能同时打开，所以打开热点的时候需要关闭wifi
			wifiManager.setWifiEnabled(false);
		}
		try {
			// 热点的配置类
			WifiConfiguration apConfig = new WifiConfiguration();
			// 配置热点的名称(可以在名字后面加点随机数什么的)
			apConfig.SSID = "dpcap01";
			// 配置热点的密码
			apConfig.preSharedKey = "12345678";
			apConfig.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);
			// 配置热点加密方式wpa
			apConfig.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
			apConfig.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
			apConfig.status = WifiConfiguration.Status.ENABLED;
			// 通过反射调用设置热点
			Method method = wifiManager.getClass().getMethod("setWifiApEnabled",
				WifiConfiguration.class, Boolean.TYPE);
			// 返回热点打开状态
			return (Boolean) method.invoke(wifiManager, apConfig, enabled);
		}
		catch (Exception e) {
			return false;
		}
	}

	public void checkLinked() {
		linkedList = new ArrayList<String>();
		try {
			BufferedReader br = new BufferedReader(new FileReader("/proc/net/arp"));
			String line;
			while ((line = br.readLine()) != null) {
				Log.i("APS", line);
				String[] splitted = line.split("\\s{2,}");
				if (splitted != null && splitted.length >= 4) {
					String ip = splitted[0];
					String mac = splitted[3];
					if (ip.equals("IP address")) {
						continue;
					}
					new Thread(new LinkTask(ip)).start();
				}
			}

		}
		catch (Exception e) {
			e.printStackTrace();
		}
	}

	public List<String> getLinked() {
		return linkedList;
	}

	public boolean isReachable(String ip) {
		boolean status = false;
		try {
			status = InetAddress.getByName(ip).isReachable(1000);
		}
		catch (UnknownHostException e) {
			e.printStackTrace();
		}
		catch (IOException e) {
			e.printStackTrace();
		}
		return status;
	}

	private class LinkTask implements Runnable {
		private String ip;

		public LinkTask(String ip) {
			super();
			this.ip = ip;
		}

		public void run() {
			boolean status = isReachable(ip);
			if (status) {
				linkedList.add(ip);
			}
		}
	}

	/**
	 * 锁定WifiLock
	 * 
	 */
	public void acquireWifiLock() {
		mWifiLock.acquire();
	}

	/**
	 * 解锁WifiLock
	 * 
	 */
	public void releaseWifiLock() {
		// 判断时候锁定
		if (mWifiLock.isHeld()) {
			mWifiLock.release();
		}
	}

	/**
	 * 创建一个WifiLock
	 * 
	 */
	public void creatWifiLock() {
		mWifiLock = wifiManager.createWifiLock("Test");
	}
}